$(function(){
	
	$.post("dep_dep.action", function(data) {
		var d = (data.deps).split(',');
		$("#usli_order_dep").empty();
		$("<option value='0'>全部</option>").appendTo("#usli_order_dep");
        $.each(d,function(){  
        	$("<option value="+this+">"+this+"</option>").appendTo("#usli_order_dep");
        });
	});
	
	$("#order_result").datagrid({
          url: "order_order_list.action",
          //method:'post',
          iconCls: "icon-edit",
	      title: "订单列表",
          loadMsg:"数据装载中....",
          striped: true,//条纹
	      fitColumns: true,//自动调整单元格宽度
	      rownumbers: true,//显示行号
	      singleSelect: false,//单选
	      pagination: true,//分页
	      queryParams: {
		   "page.status": {"id":"usli_order_status","type":"text"},
		   "page.dep": {"id":"usli_order_dep","type":"text"},
		   "page.companyNum": {"id":"usli_order_companyNum","type":"text"},
		   "page.serial_number": {"id":"usli_order_serial_number","type":"text"},
		   "page.selImportant": {"id":"usli_order_selImportant","type":"text"},
		   "page.begin_time": {"id":"usli_order_begin_time","type":"datebox"},
		   "page.end_time": {"id":"usli_order_end_time","type":"datebox"},
		   "page.customNum": {"id":"usli_order_customNum","type":"text"},
		   "page.customType": {"id":"usli_order_customType","type":"text"},
		   "page.marks": {"id":"usli_order_marks","type":"text"},
		   "page.movement": {"id":"usli_order_movement","type":"text"},
		  },
		  paramsName: {
	    	page: "page.page",
	    	num: "page.num"
	      },
	      toolbar: '#usli_order',
          columns:[[
                    {field:'ck',checkbox:true},
                    {title:'订单号',field:'companyNum',width:40},    
                    {title:'客户编号',field:'customNum',width:20},
                    {title:'客户型号',field:'customType',width:30},
                    {title:'唛头',field:'marks',width:30},
                    {title:'机芯',field:'movement',width:30},
                    {title:'数量',field:'num',width:15},
                    {title:'下定时间',field:'makeTime',width:40},
                    {title:'当前部门',field:'dep',width:30},
                    {title:'停留时间',field:'time',width:50},
                    {title:'QC1',field:'qc1',width:25},
                    {title:'QC2',field:'qc2',width:25},
				]],
         view: detailview,  
         detailFormatter:function(index,row){  
             return '<div style="padding:2px"><table id="ddv-' + index + '"></table></div>';  
         },  
         onExpandRow: function(index,row){  
             $('#ddv-'+index).datagrid({  
                 url: "order_order_trace.action?companyNum="+row.companyNum+"",  
                 fitColumns:true,  
                 singleSelect:true,  
                 rownumbers:true,  
                 loadMsg:'',  
                 height:'auto',  
                 columns:[[  
	                 {title:'部门',field:'dep',width:30},    
                     {title:'到达时间',field:'regtime',width:30},
                     {title:'离开时间',field:'outtime',width:30},
                 ]],  
                 onResize:function(){  
                     $('#order_result').datagrid('fixDetailRowHeight',index);  
                 },  
                 onLoadSuccess:function(){  
                     setTimeout(function(){  
                         $('#order_result').datagrid('fixDetailRowHeight',index);  
                     },0);  
                 }  
             });  
             $('#order_result').datagrid('fixDetailRowHeight',index);  
         } 
     });
	
	//新建框
	$('#order_add_dd').dialog({
		buttons:[{
			text:'添加',
			iconCls:'icon-ok',
			handler:function(){
				if(if_ip){
					$.post("order_order_add.action", $("#order_add_form").serialize(), function(data) {
						if(data.status == "0") {
							//$('#order_add_dd').dialog('close');
							alert(data.mess);
							$("#cn").html(data.comNum);
							$("#barcode").html("<img src='/yhm_ssh"+data.file+"' width='382' height='106'>");
						} else {
							alert(data.mess);
						}
						//if(data.pplb_title == "1"){
						//	alert("打印机打印失败");
						//}
					});
				}else{
					alert('无权限');	
				}	
			}
		},{
			text:'&nbsp;取消&nbsp;',
			handler:function(){
				$('#order_add_dd').dialog('close');
				$('#order_result').datagrid('reload');
			}
		}]
	});
	
	//修改框
	$('#order_update_dd').dialog({
		buttons:[{
			text:'修改',
			iconCls:'icon-ok',
			handler:function(){
				if(if_ip){
					$.post("order_order_update.action", $("#order_update_form").serialize(), function(data) {
						if(data.status == "0") {
							$('#order_update_dd').dialog('close');
							alert(data.mess);
							$('#order_result').datagrid('reload');
						} else {
							alert(data.mess);
						}
					});
				}else{
					alert('无权限');	
				}	
			}
		},{
			text:'&nbsp;取消&nbsp;',
			handler:function(){
				$('#order_update_dd').dialog('close');
			}
		}]
	});
	
	
	$('#order_mouth_del_dd').dialog({
		buttons:[{
			text:'确定',
			iconCls:'icon-ok',
			handler:function(){
				$.messager.confirm('Confirm','批量删除订单，请慎重使用?',function(r){ 
					if(if_ip){
						$.post("order_order_mouth_del.action", $("#order_mouth_del_form").serialize(), function(data) {
							if(data.status == "0") {
								$('#order_update_dd').dialog('close');
								alert(data.mess);
								$('#order_result').datagrid('reload');
							} else {
								alert(data.mess);
							}
						});
					}else{
						alert('无权限');	
					}
					$("#pwd").attr("value","");
				});  
			}
		},{
			text:'&nbsp;取消&nbsp;',
			handler:function(){
				$('#order_mouth_del_dd').dialog('close');
				$("#pwd").attr("value","");
			}
		}]
	});
	
	//自定义提示
	 $.fn.datebox.defaults.formatter = function (date) {
         var y = date.getFullYear();
         var m = date.getMonth() + 1;
         var d = date.getDate();
         return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
     };
     $.fn.datebox.defaults.parser = function (s) {
         if (s) {
             var a = s.split('-');
             var d = new Date(parseInt(a[0]), parseInt(a[1]) - 1, parseInt(a[2]));
             return d;
         } else {
             return new Date();
         }
     };
	
});	



function order_add_dd() {
	$('#order_add_dd').dialog('open').dialog('setTitle','新建订单');
}

function order_mouth_del_dd() {
	$('#order_mouth_del_dd').dialog('open').dialog('setTitle','管理');
}

//修改
function order_update_dd() {
	var row = $('#order_result').datagrid('getSelected');
	var ck_com = [];
	var i=0;
	$("[name=ck][type=checkbox]:checked").each(function(){
		i++;
	});
	if(i!=1){
		alert("修改订单不能多选！！");
		return;
	}
	$.post("order_get_order.action", {"companyNum":row.companyNum}, function(data) {
		$("#companyNum").val(data.companyNum);
		$("#num").val(data.num);
		$("#customNum").val(data.customNum);
		$("#customType").val(data.customType);
		$("#marks").val(data.marks);
		$("#movement").val(data.movement);
		$("#cn_up").html(data.comNum);
		$("#barcode_up").html("<img src='/yhm_ssh"+data.file+"' width='382' height='106'>");
	});
	$('#order_update_dd').dialog('open').dialog('setTitle','修改订单');
}

//删除
//function order_del() {
//	$.messager.confirm('Confirm','你是否要删除该订单信息?',function(r){
//		var row = $('#usli_device_manager').datagrid('getSelected');
//		if(if_ip){
//		    if (r){  
//		        $.post("order_order_del.action", {"companyNum":row.companyNum}, function(data) {
//					if(data.status == 0) {
//						alert(data.mess);
//						$('#order_result').datagrid('reload');
//					} else {
//						alert(data.mess);
//					}
//				}); 
//		    }
//		}else{
//			alert('无权限');	
//		}    
//	});  
//}

//多选删除
function order_del_s() {
	$.messager.confirm('Confirm','你是否要删除选择的订单信息?',function(r){ 
		if(if_ip){
		    if (r){
		    	var ck_com = [];
		    	$("[name=ck][type=checkbox]:checked").each(function(){
		    		ck_com.push($(this).val());
		    	});
		        $.post("order_order_del_s.action", {"ck_com":ck_com.toString()}, function(data) {
					if(data.status == 0) {
						$('#order_result').datagrid('reload');
					} else {
						alert(data.mess);
					}
				}); 
		    } 
		}else{
			alert('无权限');	
		} 
	});  
}


//刷新
function order_load() {
	$('#order_result').datagrid('reload');
}

//下载xls
function create_order_xls() {
	var status = $("#usli_order_status").val();
	var dep = $("#usli_order_dep").val();
	var companyNum = $("#usli_order_companyNum").val();
	var serial_number = $("#usli_order_serial_number").val();
	var selImportant = $("#usli_order_selImportant").val();
	var begin_time = $("#usli_order_begin_time").val();
	var end_time = $("#usli_order_end_time").val();
	var customNum = $("#usli_order_customNum").val();
	var customType = $("#usli_order_customType").val();
	var marks = $("#usli_order_marks").val();
	var movement = $("#usli_order_movement").val();
	location.href = 'xls_create_order_list_xls.action?page.status=' + status
			+ '&page.dep=' + dep + '&page.companyNum=' + companyNum
			+ '&page.serial_number=' + serial_number + '&page.selImportant='
			+ selImportant + '&page.begin_time=' + begin_time
			+ '&page.end_time=' + end_time + '&page.customNum=' + customNum
			+ '&page.customType=' + customType + '&page.marks=' + marks
			+ '&page.movement=' + movement + '';
}


function formSubmit() {
	$("#afupmes").html("");
	$("#loading").show();
	uploadbutton.submit();
}



